package vehement.game;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.Project;

import controller.Controller;


public class GameThread implements Runnable {

	public GameThread(){
		world = new World(10,10);
		gameOver = false;
	}
	
	//this will call updates
	public void run() {
		while (!gameOver){

			
			//We find the amount of time since last update
			long time = System.nanoTime();
			float delta = (time - lastTime) / 1000000000.f;
			
			//lets look for input
			Controller.wiggleKeyboard(delta);
			
			//lets update our world
			
			world.update(delta);
			
			lastTime = time;
		
			try{
				Thread.sleep(10); //this ensures we are giving time to other threads.
				
			}catch (Exception e){
				//TODO: im not sure what, but we should not ignore this
				e.printStackTrace();
			}

		}
	}
	public void draw(){
		if (!gameOver){
			
			//this clears the last frame.
			GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
		
			GL11.glPushMatrix();
			
			//Camera
			Project.gluLookAt(Globals.camPos.x,Globals.camPos.y, Globals.camPos.z, Globals.camTarg.x, Globals.camTarg.y, Globals.camTarg.z, 0.f, 0.f, 1.f);
			world.draw();
			GL11.glPopMatrix();
		
			Display.update();
		}
	}

	/**
	 * This stops the thread.  
	 */
	public void gameOver(){
		//TODO:There is a more synchronous way of doing this. The thread could be partway through run
		gameOver = true;
	}
	
	public static World world;
	public static long lastTime;
	private static boolean gameOver;
}
